package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementDTO;
import com.neusoft.neuiotms.dcom.model.DutyArrangement;
import com.neusoft.neuiotms.dcom.model.DutyArrangementGroup;
import com.neusoft.neuiotms.dcom.model.FunctionalGroup;
import com.neusoft.neuiotms.dcom.model.SchedulingRule;
import com.neusoft.neuiotms.dcom.repository.DutyArrangementGroupRepository;
import com.neusoft.neuiotms.dcom.repository.DutyArrangementRepository;
import com.neusoft.neuiotms.dcom.repository.FunctionalGroupRepository;
import com.neusoft.neuiotms.dcom.repository.SchedulingRuleRepository;

@Component
public class DutyArrangementConverter extends AbstractConverter<DutyArrangement, DutyArrangementDTO> {

    @Autowired
    private DutyArrangementRepository dutyArrangementRepository;
    @Autowired
    private DutyArrangementGroupConverter dutyArrangementGroupConverter;
    @Autowired
    private DutyArrangementGroupRepository dutyArrangementGroupRepository;
    @Autowired
    private FunctionalGroupRepository functionalGroupRepository;
    @Autowired
    private SchedulingRuleRepository schedulingRuleRepository;

    @Override
    public DutyArrangement toModel(final DutyArrangementDTO dto) {

        DutyArrangement model;

        if (dto.isNew()) {
            model = new DutyArrangement();
        } else {
            model = this.dutyArrangementRepository.getOne(dto.getId());
        }

        // 设置职能
        if (null != dto.getFunctionalGroupId()) {
            final FunctionalGroup functionalGroupModel =
                    this.functionalGroupRepository.getOne(dto.getFunctionalGroupId());
            model.setFunctionalGroup(functionalGroupModel);
        }

        // 设置规则
        if (null != dto.getSchedulingRuleId()) {
            final SchedulingRule schedulingRuleModel = this.schedulingRuleRepository.getOne(dto.getSchedulingRuleId());
            model.setSchedulingRule(schedulingRuleModel);
        }

        model.setName(dto.getName());
        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setDeleteFlag(dto.getDeleteFlag());

        // 生成组的信息,先删除原来的信息，再生成新的消息
        final List<DutyArrangementGroup> groups = model.getGroups().stream().collect(Collectors.toList());
        groups.stream().forEach(k -> {
            model.getGroups().remove(k);
            this.dutyArrangementGroupRepository.delete(k);
        });

        dto.getGroups().stream().map(k -> {
            final DutyArrangementGroup groupModel = this.dutyArrangementGroupConverter.toModel(k);
            groupModel.setGroup(model);
            return groupModel;
        }).filter(s -> null != s).forEach(model.getGroups()::add);

        return model;
    }

    @Override
    public DutyArrangementDTO toDTO(final DutyArrangement model, final boolean forListView) {

        final DutyArrangementDTO dto = new DutyArrangementDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setDeleteFlag(model.getDeleteFlag());

        // 职能
        if (null != model.getFunctionalGroup()) {
            dto.setFunctionalGroupId(model.getFunctionalGroup().getId());
            dto.setFunctionalGroupName(model.getFunctionalGroup().getName());
        }

        // 规则
        if (null != model.getSchedulingRule()) {
            dto.setSchedulingRuleId(model.getSchedulingRule().getId());
            dto.setSchedulingRuleName(model.getSchedulingRule().getName());
            dto.setClassesId(model.getSchedulingRule().getClasses().getId());
        }

        if (!forListView) {
            // 组的详细信息
            model.getGroups().stream().map(k -> this.dutyArrangementGroupConverter.toDTO(k)).filter(s -> null != s)
                    .forEach(dto.getGroups()::add);
        }

        return dto;
    }

}
